/**
 * Created by albert on 2017/11/2.
 */
import Api from "../../api/api"
import * as types from "../mutation-types"
import  {setParams} from "../../util/methods"
const state = {
    param: {
        "name": "",
        "phone": "",
        "store_name": "",
        "city": "",
        "yqType": null,
        "page": 0
    },
    overdueList: [],
    overdueDetail:{
        "lateFee": 0,
        "overDueDays": 0,
        "had_pay_periods": 0,
        "hadnot_pay_periods": 0,
        "periods": 0,
        "contract_no": "",
        "applicant_name": "",
        "SNAME": "",
        "ci_loan_money": "",
        "ci_periods": 0,
        "mobile": "",
        "create_time": ""
    },
    detailParam:{
        "contractNo": ""
    },
    followInfoParam:{
        "contract_no": "",
        "page": 1
    },
    overdueFollowInfo: [],
    overdueFollowInfoadd:{
        "contract_no":"",
        "cate": "",
        "description": ""
    },
    addResult:{
        "lastId": ""
    } 
}
const actions = {
    overdueList({commit}, payload){
        var append = payload.hasOwnProperty("append") ? payload.append : false
        return new Promise((resolve, reject) => {
            Api.overdueList(state.param)
                .then(res => {
                    commit({
                        type: "OVERDUE_LIST",
                        append: append,
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    overdueDetail({commit}, payload){
        var append = payload.hasOwnProperty("append") ? payload.append : false
        return new Promise((resolve, reject) => {
            Api.overdueDetail(state.detailParam)
                .then(res => {
                    commit({
                        type: "OVERDUE_DETAIL",
                        append: append,
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    overdueFollowInfo({commit}, payload){
        var append = payload.hasOwnProperty("append") ? payload.append : false
        return new Promise((resolve, reject) => {
            Api.overdueFollowInfo(state.followInfoParam)
                .then(res => {
                    commit({
                        type: "OVERDUE_FOLLOWINFO",
                        append: append,
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    overdueFollowInfoAdd({commit}, payload){
        var append = payload.hasOwnProperty("append") ? payload.append : false
        return new Promise((resolve, reject) => {
            Api.overdueFollowInfoAdd(state.overdueFollowInfoadd)
                .then(res => {
                    commit({
                        type: "OVERDUE_FOLLOWINFOADD",
                        append: append,
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
}

const mutations = {
    setValue(state, payload){
        state.param[payload.key] = payload.value
    },
    setDetailValue(state, payload){
        state.detailParam.payload.key = payload.value
    },
    setOverdueParams(state, payload){
        state=setParams(state,payload.params)
    },
    setDetailParams(state, payload){
        state=setParams(state,payload.params)
    },
    setOverdueFollowInfo(state, payload){
        state=setParams(state,payload.params)
    },
    setOverdueFollowInfoAdd(state, payload){
        state=setParams(state,payload.params)
    },
    resetParam(state){
        state.params = {
            "name": "",
            "phone": "",
            "store_name": "",
            "city": "",
            "yqType": null,
            "page": 0
        };
        state.overdueList = [];
    },
    resetDetailParam(state){
        state.detailParam = {
            "contractNo": "",
        };
        state.overdueDetail = null;
    },
    resetOverdueFollowInforesetDetailParam(state){
        state.detailParam = {
            "contractNo": "",
        };
        state.overdueFollowInfo = [];
    },
    resetOverdueFollowInforesetDetailParam(state){
        state.detailParam = {
            "contractNo": "",
        };
        state.overdueFollowInfoadd = {
            "contract_no":"",
            "cate": "",
            "description": ""
        };
    },
    [types.OVERDUE_LIST](state, payload){
            if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
                state.overdueList = payload.res.data;
            } else {
                state.overdueList = state.overdueList.concat(payload.res.data)
            }   
    },
    [types.OVERDUE_DETAIL](state, payload){
            if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
                state.overdueDetail = payload.res;
            } else {
                state.overdueDetail = Object.assign(state.overdueDetail,payload.res)
            }   
    },
    [types.OVERDUE_FOLLOWINFO](state, payload){
        if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
            state.overdueFollowInfo = payload.res.data;
        } else {
            state.overdueFollowInfo = state.overdueFollowInfo.concat(payload.res.data)
        }   
    },
    [types.OVERDUE_FOLLOWINFOADD](state, payload){
        if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
            state.overdueFollowInfoAdd = Object.assign(state.addResult,payload.res.data);
        } else {
            state.overdueFollowInfoAdd = Object.assign(state.addResult,payload.res.data)
        }   
    }
}
export default {
    state,
    actions,
    mutations,
}